//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CdsHelper.h"
using namespace Cephei::QL::Termstructures::Credit;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/DefaultProbabilityTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Credit::CCdsHelper::CCdsHelper (boost::shared_ptr<QuantLib::CdsHelper>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCdsHelper = NULL;
#endif
	_ppCdsHelper = &childNative;
    
}
Cephei::QL::Termstructures::Credit::CCdsHelper::CCdsHelper (QuantLib::CdsHelper& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCdsHelper = NULL;
#endif
	_ppCdsHelper = new boost::shared_ptr<QuantLib::CdsHelper> (&childNative);
    
    _CdsHelperOwner = owner;
    
}

Cephei::QL::Termstructures::Credit::CCdsHelper::CCdsHelper (CCdsHelper^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCdsHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCdsHelper = new boost::shared_ptr<QuantLib::CdsHelper> (copy->GetShared());
        
    }
}
Cephei::QL::Termstructures::Credit::CCdsHelper::CCdsHelper (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCdsHelper = NULL;
#endif
	if (!t->IsSubclassOf(CCdsHelper::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Credit::CCdsHelper::CCdsHelper (QuantLib::Handle<QuantLib::CdsHelper>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCdsHelper = &childNative;
	_ppCdsHelper = &static_cast<boost::shared_ptr<QuantLib::CdsHelper>>(childNative.currentLink());
    
    _CdsHelperOwner = owner;
}
Cephei::QL::Termstructures::Credit::CCdsHelper::CCdsHelper (QuantLib::Handle<QuantLib::CdsHelper> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCdsHelper = &childNative;
	_ppCdsHelper = &static_cast<boost::shared_ptr<QuantLib::CdsHelper>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Credit::CCdsHelper::CCdsHelper (QuantLib::CdsHelper childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCdsHelper = NULL;
#endif
	_ppCdsHelper = new boost::shared_ptr<QuantLib::CdsHelper> (new QuantLib::CdsHelper (childNative));
    
}
#endif

Cephei::QL::Termstructures::Credit::CCdsHelper::~CCdsHelper ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCdsHelper != NULL)
    {
	    delete _ppCdsHelper;
        _ppCdsHelper = NULL;
    }
}
Cephei::QL::Termstructures::Credit::CCdsHelper::!CCdsHelper ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCdsHelper != NULL)
    {
	    delete _ppCdsHelper;
    }
}
QuantLib::CdsHelper& Cephei::QL::Termstructures::Credit::CCdsHelper::GetReference ()
{
    if (_ppCdsHelper == NULL) throw REFNEW NativeNullException ();
	return **_ppCdsHelper;
}
boost::shared_ptr<QuantLib::CdsHelper>& Cephei::QL::Termstructures::Credit::CCdsHelper::GetShared ()
{
    if (_ppCdsHelper == NULL) throw REFNEW NativeNullException ();
	return *_ppCdsHelper;
}
QuantLib::CdsHelper* Cephei::QL::Termstructures::Credit::CCdsHelper::GetPointer ()
{
    if (_ppCdsHelper == NULL) throw REFNEW NativeNullException ();
	return &**_ppCdsHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CdsHelper>& Cephei::QL::Termstructures::Credit::CCdsHelper::GetHandle ()
{
	if (_phCdsHelper == NULL)
	{
		_phCdsHelper = new Handle<QuantLib::CdsHelper> (*_ppCdsHelper);
	}
	return *_phCdsHelper;
}
#endif
bool Cephei::QL::Termstructures::Credit::CCdsHelper::HasNative () 
{
	return (_ppCdsHelper != NULL);
}

Cephei::QL::Termstructures::Credit::ICdsHelper^ Cephei::QL::Termstructures::Credit::CCdsHelper::SetTermStructure (Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ ts)
{
    CDefaultProbabilityTermStructure^ _Cts;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cts = safe_cast<CDefaultProbabilityTermStructure^> (ts);
        _Cts->Lock();
        QuantLib::DefaultProbabilityTermStructure* _ts = static_cast<QuantLib::DefaultProbabilityTermStructure*> (_Cts->GetPointer ()); 
    	(*_ppCdsHelper)->setTermStructure ( _ts );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cts != nullptr) _Cts->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

